package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author AgusNH
 */
public class ModelAkun {

    Connection conn;

    public ModelAkun(Connection conn) {
        this.conn = conn;
    }

    public List<Users> getAllUsers() throws Exception {
        List<Users> users = new ArrayList<Users>();
        String sql = "SELECT * FROM users";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Users user = new Users();
                user.setIdUser(rs.getInt(1));
                user.setNama(rs.getString(2));
                user.setAlamat(rs.getString(3));
                user.setTelepon(rs.getString(4));
                user.setEmail(rs.getString(5));
                user.setUsername(rs.getString(6));
                user.setPassword(rs.getString(7));
                user.setAdminStat(rs.getInt(8));
                users.add(user);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return users;
    }

    public Users getUser(String username) {
        Users user = new Users();
        String sql = "SELECT * FROM users WHERE USERNAME = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setString(1, username);
            ResultSet rs = st.executeQuery();
            if (rs != null) {
                if (rs.next()) {
                    user.setIdUser(rs.getInt(1));
                    user.setNama(rs.getString(2));
                    user.setAlamat(rs.getString(3));
                    user.setTelepon(rs.getString(4));
                    user.setEmail(rs.getString(5));
                    user.setUsername(rs.getString(6));
                    user.setPassword(rs.getString(7));
                    user.setAdminStat(rs.getInt(8));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public void insertUser(String nama, String alamat, String telepon, String email, String username, String password) throws Exception {
        String sql = "INSERT INTO users VALUES (NULL , ?, ?, ?, ?, ?, ?, 0)";
        try {
            PreparedStatement st = conn.prepareStatement(sql);

            st.setString(1, nama);
            st.setString(2, alamat);
            st.setString(3, telepon);
            st.setString(4, email);
            st.setString(5, username);
            st.setString(6, password);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateUser(String nama, String alamat, String telepon, String email, String password, String username) throws Exception {
        String sql = "UPDATE users SET NAMA=?, ALAMAT=?, TELEPON=?, EMAIL=?, PASSWORD=? WHERE USERNAME=?";
        try {
            PreparedStatement st = conn.prepareStatement(sql);
            st.setString(1, nama);
            st.setString(2, alamat);
            st.setString(3, telepon);
            st.setString(4, email);
            st.setString(5, password);
            st.setString(6, username);
            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void deleteUser(int idUser) {

        String sql = "DELETE FROM users WHERE ID_USER = ?";
        try {
            PreparedStatement st = this.conn.prepareStatement(sql);
            st.setInt(1, idUser);

            st.executeUpdate();
            st.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
